import '../../lib_runtime/event_center.dart';
import '../../lib_runtime/event_constant.dart';
import '../../lib_runtime/global_config.dart';
import '../../lib_runtime/logger.dart';
import '../../m_chatroom/src/interface/root_proxy_model.dart';
import '../../m_login/util/login_manager.dart';

///点击隐私协议同意之后初始化的内容
class MainInit {
  static initIM() async {
    //必现要等im初始化之后，才可以调用im相关api
    // await IMManager.get().init();
    var loginListener = LoginManager.instance.loginListener;
    loginListener
      ..onLogin = (user) async {
        //用户登录
        RLog.d("loginListener.onLogin() called");
        // await IMManager.get().autoLogin(user!.emUsername, user.emPwd);
      }
      ..onLogout = (user) async {
        // 用户退出
        RLog.d("loginListener.onLogout() called");
        // await IMManager.get().logout();

        // 用户退出登录后，关闭房间
        await RootProxyModel.instance?.release();
      }
      ..onUpdateUserInfo = (user) async {
        if (user == null) {
          return;
        }
        //更新用户信息
        // RLog.d(
        //     "loginListener.onUpdateUserInfo() called user:${user?.toProto3Json()}");
        // await IMManager.get().loginUser.updateIMUserInfo(
        //       userId: user.id.toString(),
        //       nickname: user.nickname,
        //       avatarUrl: user.avatar,
        //       isMale: user.gender == UserConstants.genderMan,
        //       birth: user.birthday,
        //       signature: user.personalSignature,
        //     );
      };
  }

  static registerEvent() {
    eventCenter
        .subscribes([EventConstant.refreshConfInfo]).listen(_eventCallback);
  }

  static Future<bool> appInit() async {
    RLog.d("appInit");
    registerEvent();
    // Shanmeng.ins.init(1687776105,'f92274299d570735036e8d29588eb283');
    // Shanmeng.ins.enable = false;

    /// 数数埋点初始化
    // await Reporter.init();
    // const alog = VeAlogImpl.instance;
    // alog.enable();
    // alog.enableConsoleLog();
    await initIM();
    // await CommonApi().initSdk(InitSdkReq());
    // if (Platform.isIOS) {
    //   JRIap.instance;
    // }
    RLog.d("begin auto login...", "main()");
    var result = await LoginManager.instance.authLogin("onApplicationCreate");
    RLog.d("after auto login result = $result", "main()");
    return result.data ?? false;
  }

  static void _eventCallback(Event event) {
    // switch (event.name) {
    //   case EventConstant.refreshConfInfo:
    //     ConfResp? confResp = event.data;
    //     if (confResp?.hasDisableShanmeng() ?? false) {
    //       Shanmeng.ins.enable = !confResp!.disableShanmeng;
    //     }
    //     break;
    // }
  }

  ///启动初始化
  static launchInit() async {
    try {
      GlobalConfig.logLastTimeDiff('launchInit');
      // JRNetwork.instance.useHttp2 = true;
      await GlobalConfig.initCommonParam();
      await GlobalConfig.refreshConfInfo();
      bool hasLogin = await MainInit.appInit();
      RLog.i('checkVersionUpdate$hasLogin');
      await checkVersionUpdate();
      GlobalConfig.logLastTimeDiff('launchInitEnd');
    } catch (e) {
      RLog.i('launchInitCatch:$e');
    }
  }

  static checkVersionUpdate() async {
    // var result = await AppApi.instance.getVersionUpdateInfo();
    // if (result.isSuccess &&
    //     result.data != null &&
    //     result.data!.updateMod != UpdateMode.NOT_REMIND &&
    //     result.data!.updateLink.isNotEmpty) {
    //   var platform = await PackageInfo.fromPlatform();
    //   if (result.data!.latestVersion == platform.version) {
    //     showToast(T.current!.already_latest_version);
    //   } else {
    //     await VersionUpdateWidget.show(
    //         rootNavigatorState.currentContext!, result.data!);
    //   }
    //   return true;
    // } else {
    //   return true;
    // }
  }
}
